var form;
var layer;
//页面初始化时加载Layui框架
layui.use(['form', 'layer', 'laydate'], function () {
    form = layui.form();
    layer = layui.layer;

    $.removeTableCookie('SpryMedia_DataTables_productTable_productList.jsp');
    getDataTable();

    // 查询
    $("#search").click(function () {
        $.removeTableCookie('SpryMedia_DataTables_productTable_productList.jsp');
        getDataTable(); //要触发的方法
        return false;
    });

    //回车事件
    $(document).keydown(function (event) {
        var code = event.which;
        if (code == 13) {
            $.removeTableCookie('SpryMedia_DataTables_productTable_productList.jsp');
            getDataTable(); //要触发的方法
            return false;
        }
    });
});
/**
 * 获取列表
 */
function getDataTable() {
    $('#productTable').dataTable({
        'bProcessing': true,
        'bServerSide': true,
        'fnServerParams': function (aoData) {
            var pId = $.trim($("#pId").val());
            var productCode = $("#productCode").val();
            aoData.push({
                "name": "product.productionId",
                "value": pId
            }, {
                "name": "product.code",
                "value": productCode
            });
        },
        "sAjaxSource": basePath + "production/getProductList.action", // 获取列表
        "sServerMethod": "POST",
        "sPaginationType": "full_numbers",
        "bPaginate": true, // 翻页功能
        "bStateSave": true, // 状态保存
        "bLengthChange": false, // 改变每页显示数据数量
        "bFilter": false, // 过滤功能
        "bSort": false, // 排序功能
        "bInfo": true, // 页脚信息
        "bAutoWidth": false, // 自动宽度
        "bDestroy": true,
        "iDisplayLength": 15, // 每页显示多少行
        "aoColumns": [{
            "sTitle": '排序',
            "mDataProp": "no"
        }, {
            "sTitle": '生产批次',
            "mDataProp": "batch"
        }, {
            "sTitle": '产品类型',
            "mDataProp": "productTypeStr"
        }, {
            "sTitle": '产品编号',
            "mDataProp": "code"
        }, {
            "sTitle": '产品名称',
            "mDataProp": "name",
            "sClass": "conceal"
        }, {
            "sTitle": '焊接',
            "mDataProp": "weldName"
        }, {
            "sTitle": '组装',
            "mDataProp": "assembleName"
        }, {
            "sTitle": '调试',
            "mDataProp": "debugName"
        }, {
            "sTitle": '质检',
            "mDataProp": "checkName"
        }, {
            "sTitle": '质检时间',
            "mDataProp": "checkTime"
        }, {
            "sTitle": '质检结果',
            "mDataProp": 'checkResultStr'
        }, {
            "sTitle": '库存状态',
            "mDataProp": "stockStateStr"
        }, {
            "sTitle": '出入库时间',
            "mDataProp": "outOrInTime"
        }],
        "oLanguage": {
            "sUrl": basePath + "plugins/datatable/cn.txt" // 中文包
        },
        "fnDrawCallback": function (oSettings) {
            $('#productTable tbody  tr td').each(function () {
                this.setAttribute('title', $(this).text());
            });
        },
        "fnInitComplete": function () {
        },
        "aoColumnDefs": [{
            "aTargets": [3],
            "fnRender": function (oObj) {
                var proId = oObj.aData.id;
                var code = oObj.aData.code;
                return '<a href="javascript:void(0);" onclick=detailProduct("' + proId + '")>' + code + '</a>';
            }
        }]
    });
}

/**
 * 查看产品详情
 * @param proId 产品Id
 */
function detailProduct(proId){
    var url = "production/toDetail.action?type=detail&id=" + proId;
    layer.open({
        type: 2,
        title: '查看产品详情',
        area: ["60%", "75%"],
        shade: 0.3,
        maxmin: true,
        content: basePath + url,
        btn: ['关闭'],
        yes: function (index) {
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        },
        zIndex: layer.zIndex,
        success: function (layero) {
            layer.setTop(layero); //弹窗置于最上层
        }
    });
}
